<?php
/**
 * Helper for php codesniffer log formating.
 *
 * @category PhpCilux
 * @package  PhpCilux
 */

/**
 * Helper for php codesniffer log formating.
 *
 * @package PhpCilux
 */
class Zend_View_Helper_PhpCs extends Zend_View_Helper_Abstract
{

    /**
     * Formating the  php unit log.
     *
     * @param string $content The log content which will be styled.
     * @param string $srcDir  The path of the source dir.
     *
     * @return string
     */
    public function phpCs($content, $srcDir)
    {
        $content = str_replace($srcDir, '', $content);
        $lines   = explode("\n", $content);
        $content = '<h4>PHP_CodeSniffer</h4>';
        $errors  = 0;

        $totalLines = count($lines);

        while (list($key, $line) = each($lines)) {
            $line = rtrim($line);

            switch(true) {
                case false !== stripos($line, '|       |'):
                case false !== stripos($line, '| ERROR |'):
                    $content .= '<div class="failed">' . $line . '</div>';
                    $errors++;
                    break;

                case 'FILE' == substr($line, 0, 4) && $key > 1:
                    $content .= "\n" . $line . "\n";
                    break;

                case false !== stripos($line, 'ERROR(S) AFFECTING'):
                case '-' == $line[1] && '' == trim($line, '-'):
                case '' == trim($line):
                    break;

                default:
                    $content .= $line . "\n";
                    break;
            }
        }

        if ($errors > 0) {
            $content .= "\n" . '<div class="failed">FAILED with ' . $errors . ' error(s)</div>';
        } else {
            $content .= "\n" . '<div class="success">OK</div>';
        }

        return $content;
    }

}